package com.example.yichuangyun.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.mvvm.ui.activity.CompanyDetailsActivity;
import com.google.gson.Gson;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RequestUtils {

    /**
     * 添加跟进
     */
    public static void addFollow(String businessId, String clientName, String mTelNumber) {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("followName", clientName);
            requestDate.put("followTel", mTelNumber);
            requestDate.put("followType", 2);
            requestDate.put("followTime", DateUtil.getNow());
            requestDate.put("chanceId", businessId);
            requestDate.put("nextFollowTime", "");
            requestDate.put("followRemark", "拨号跟进");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().addFollowUp(requestBody), new MyObserver<HttpRespond<String>>() {
            @Override
            protected void onSuccess(HttpRespond<String> stringHttpRespond) {
                if (Constant.SUCCESS == stringHttpRespond.code) {
//                    ToastHelper.show("添加跟进成功");
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshFollowList));
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshInformationDetail));
                } else {
                    ToastHelper.show(stringHttpRespond.msg);
                }
            }
        });
    }

    /**
     * 添加联系人跟进
     */
    public static void addContactsFollow(String contactId, String clientName, String mTelNumber) {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("followName", clientName);
            requestDate.put("followTel", mTelNumber);
            requestDate.put("followType", 2);
            requestDate.put("followTime", DateUtil.getNow());
            requestDate.put("contactId", contactId);
            requestDate.put("nextFollowTime", "");
            requestDate.put("followRemark", "拨号跟进");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().addContactsFollowUp(requestBody), new MyObserver<HttpRespond<String>>() {
            @Override
            protected void onSuccess(HttpRespond<String> stringHttpRespond) {
                if (Constant.SUCCESS == stringHttpRespond.code) {
//                    ToastHelper.show("添加跟进成功");
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshFollowList));
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactDetailsData));
                } else {
                    ToastHelper.show(stringHttpRespond.msg);
                }
            }
        });
    }

    public static void callNum(Context context, String mTelNumber) {
        if (!XXPermissions.isGrantedPermission(context, Permission.CALL_PHONE)) {
            XXPermissions.with(context)
                    .permission(Permission.CALL_PHONE)
                    .request((permissions, all) -> {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + mTelNumber));
                        context.startActivity(intent);
                    });
        } else {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + mTelNumber));
            context.startActivity(intent);
        }
    }

    /**
     * 回拨外呼
     */
    public static void callbackOutbound(Context context, String mTelNumber, String businessId, String clientName) {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("type", 10);
            requestDate.put("mobile", mTelNumber);
            if (!TextUtils.isEmpty(businessId)) {
                requestDate.put("chanceId", businessId);
            }
            if (!TextUtils.isEmpty(clientName)) {
                requestDate.put("userName", clientName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().callbackOutbound(requestBody), new MyObserver<HttpRespond<String>>() {
            @Override
            protected void onSuccess(HttpRespond<String> stringHttpRespond) {
                ToastHelper.show(Constant.SUCCESS == stringHttpRespond.code ? context.getString(R.string.call_success) : stringHttpRespond.msg);
            }
        });
    }

    /**
     * 联系人回拨外呼
     */
    public static void callbackOutbound2(Context context, String mTelNumber, String contactId, String clientName) {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("type", 10);
            requestDate.put("mobile", mTelNumber);
            requestDate.put("contactId", contactId);
            if (!TextUtils.isEmpty(clientName)) {
                requestDate.put("userName", clientName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().callbackOutbound(requestBody), new MyObserver<HttpRespond<String>>() {
            @Override
            protected void onSuccess(HttpRespond<String> stringHttpRespond) {
                ToastHelper.show(Constant.SUCCESS == stringHttpRespond.code ? context.getString(R.string.call_success) : stringHttpRespond.msg);
            }
        });
    }

    /**
     * 检查电话是否黑名单
     *
     * @param mTelNumber 电话号
     * @param isShow     手机号是否脱敏
     */
    public static void checkCallNum(Context context, String mTelNumber, MyDialog myDialog, int flag, boolean isShow) {
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().checkPhone(mTelNumber), new MyObserver<HttpRespond<Boolean>>() {
            @Override
            protected void onSuccess(HttpRespond<Boolean> booleanHttpRespond) {
                if (Constant.SUCCESS == booleanHttpRespond.code) {
                    if (booleanHttpRespond.data) {
                        ToastHelper.show(context.getString(R.string.call_fail));
                    } else {
                        if (flag == 0) {
                            myDialog.setText(isShow ? mTelNumber : CommonUtil.getStarPhone(mTelNumber), context.getString(R.string.confirm_call_num), context.getString(R.string.direct_call), context.getString(R.string.callback_outbound));
                            myDialog.showDialog();
                        } else {
                            if (!XXPermissions.isGrantedPermission(context, Permission.CALL_PHONE)) {
                                XXPermissions.with(context)
                                        .permission(Permission.CALL_PHONE)
                                        .request((permissions, all) -> {
                                            Intent intent = new Intent();
                                            intent.setAction(Intent.ACTION_CALL);
                                            intent.setData(Uri.parse("tel:" + mTelNumber));
                                            context.startActivity(intent);
                                        });
                            } else {
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_CALL);
                                intent.setData(Uri.parse("tel:" + mTelNumber));
                                context.startActivity(intent);
                            }
                        }
                    }
                }
            }
        });
    }

    public static void getEidByCreditNo(Activity activity, String creditNo) {
        if (TextUtils.isEmpty(creditNo)) {
            ToastHelper.show(activity.getString(R.string.no_company_info));
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("creditNo", creditNo);
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(map));
            RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getEid(requestBody), new MyObserver<HttpRespond<CompanyDataModel.CompanyModel>>() {
                @Override
                protected void onSuccess(HttpRespond<CompanyDataModel.CompanyModel> companyModelHttpRespond) {
                    if (Constant.SUCCESS == companyModelHttpRespond.code) {
                        if (null != companyModelHttpRespond.data) {
                            String eid = companyModelHttpRespond.data.getEid();
                            Bundle bundle = new Bundle();
                            bundle.putString(Constant.eidKey, eid);
                            JumpIntent.jump(activity, CompanyDetailsActivity.class, bundle);
                        } else {
                            ToastHelper.show(activity.getString(R.string.no_company_info));
                        }
                    } else {
                        ToastHelper.show("服务开小差了，请稍后重试...");
                    }
                }
            });
        }
    }
}
